package com.mrd.gtimp.v2.datalock;

import com.mrd.redis.JedisClientClusterImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.annotation.Resource;

@Component
public class DataLockUtil {
    @Resource
    private JedisClientClusterImpl jedisClientCluster;

    private static final String DATALOCKKEY = "dataLock";

    public DataLockEntity getLockInfo(String opaId, String bidId) {
        String mid = DATALOCKKEY + opaId + bidId;
        return jedisClientCluster.get(mid, DataLockEntity.class);
    }

    public String getOpaIDByKey(String key) {
        return "VDBS";
    }

    private void log(String msg) {
        System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ":\t" + msg);
    }

    public boolean lock(String opaId, String bidId, String regUser) {
        // fix P02040
        if (StringUtils.isEmpty(bidId)) {
            return true;
        }
        String mid = DATALOCKKEY + opaId + bidId;
        DataLockEntity dataLockEntity = jedisClientCluster.get(mid, DataLockEntity.class);
        if ((null != dataLockEntity) && !dataLockEntity.getRegUser().equals(regUser)) {
            log("lock fail. data alread lock:" + bidId);
            return false;
        } else {
            log("lock success.:" + bidId);
            jedisClientCluster.set(mid, new DataLockEntity(mid, new Date(), regUser, bidId, opaId, ""), null, null);
            return true;
        }
    }

    public void releaseLock(String opaId, String bidId) {
        log("lock release:" + bidId);
        jedisClientCluster.delete(DATALOCKKEY + opaId + bidId);
    }

    public ConcurrentMap<String, DataLockEntity> list() {
        Collection keys = jedisClientCluster.keys(DATALOCKKEY);
        ConcurrentHashMap<String, DataLockEntity> dataLockMap = new ConcurrentHashMap();
        for (Object key : keys) {
            if (key instanceof String) {
                dataLockMap.put(key.toString().replace(DATALOCKKEY, ""), jedisClientCluster.get(key.toString(), DataLockEntity.class));
            }
        }
        return dataLockMap;
    }


    public boolean tryLock(String opaId, String bidId, String regUser) {
        // fix P02040
        if (StringUtils.isEmpty(bidId)) {
            return true;
        }
        String mid = DATALOCKKEY + opaId + bidId;
        DataLockEntity dataLockEntity = jedisClientCluster.get(mid, DataLockEntity.class);
        if (null != dataLockEntity && !dataLockEntity.getRegUser().equals(regUser)) {
            log("lock fail. data alread lock:" + bidId);
            return false;
        } else {
            return true;
        }
    }

    public boolean tryLockDeclaration(String bidId, String regUser) {
        return tryLock(getOpaIDByKey("opa.view_declaration_by_sup"), bidId, regUser);
    }
}
